package com.carkeeper.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carkeeper.dto.UserDTO;
import com.carkeeper.service.OrderLogService;
import com.carkeeper.service.OrderService;
import com.carkeeper.entity.OrderPO;
import com.carkeeper.entity.OrderLogPO;
import com.carkeeper.service.UserFeign;
import com.carkeeper.vo.OrderLogVO;
import com.carkeeper.vo.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/orderLog")
public class OrderLogController {
    @Autowired
    private OrderLogService orderLogService;

    @Autowired
    private OrderService orderService;
    @Autowired
    private UserFeign userFeign;
    /*
    * 新增订单日志
    * */
    @PostMapping
    public String addOrderLog(@RequestBody OrderLogVO orderLogVo){
        OrderLogPO orderLogPO = new OrderLogPO();
        OrderPO order = null;
        orderLogPO.setCreateTime(LocalDateTime.now());
        orderLogPO.setCreateTime(LocalDateTime.now());
        if(orderLogVo.getDescription()!=null){
            orderLogPO.setDescription(orderLogVo.getDescription());
        }
        if(orderLogVo.getOrderId()!=null){
            orderLogPO.setOrderId(orderLogVo.getOrderId());
            order = orderService.getOrder(orderLogVo.getOrderId());
        }
        if(orderLogVo.getUserId()!=null){
            orderLogPO.setUserId(orderLogVo.getUserId());
        }
        orderLogPO.setHandleTimeJdk(new Date());
        if(order!=null && order.getOrderStatus() != null){
            orderLogPO.setOrderStatus(order.getOrderStatus());
        }
        UserDTO user = userFeign.getUser(orderLogVo.getUserId());
        orderLogPO.setUserRealName(user.getRealname());

        try {
            orderLogService.addOrderLog(orderLogPO);
            return "新增订单日志成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "新增订单日志失败";
        }
    }

    /*
    * 获取订单日志分页数据
    * */
    @GetMapping
    public Pager<OrderLogPO> getOrderLogPage(@RequestBody Pager<OrderLogVO> pager){
        if(pager != null){
            Page<OrderLogPO> page = new Page<>(pager.getPage(),pager.getPageSize());
            Pager<OrderLogPO> orderLogPage = orderLogService.getOrderLogPage(page);
            return orderLogPage;
        }
        return null;
    }

    /*
    * 获取订单日志详情
    * */
    @GetMapping("/{id}")
    public OrderLogPO getOrderLog(@PathVariable("id") Long id){
        try {
            OrderLogPO orderLogPO = orderLogService.getOrderLog(id);
            return orderLogPO;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /*
    * 删除订单日志
    * */
    @DeleteMapping("/{id}")
    public String delById(@PathVariable("id") Long id){
        try {
            orderLogService.delById(id);
            return "删除成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "删除失败";
        }
    }
}
